package com.application.fabric.test;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Tem {
    public static void main(String[] args) throws Exception {
//        String uuid = Long.toString(System.currentTimeMillis());
//        System.out.println(uuid);
        String s = encrypt(HMAC_MD5,"231","ceshi");
        System.out.println(s);
    }
public static final String HMAC_SHA1 = "HmacSHA1";
    public static final String HMAC_MD5 = "HmacMD5";
    public static final String HMAC_SHA256 = "HmacSHA256";
    public static final String HMAC_SHA512 = "HmacSHA512";

    public static String encrypt(String algorithm, String key, String message) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKeySpec);
        byte[] digest = mac.doFinal(message.getBytes("UTF-8"));
        return byte2hex(digest);
    }

    private static String byte2hex(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        String tmp = null;
        for (byte b : bytes) {
            tmp = Integer.toHexString(b & 0xFF);
            if (tmp.length() == 1) {
                builder.append("0");
            }
            builder.append(tmp);
        }
        return builder.toString();
    }
}
